package net.huashitong.supervision.task.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.service.SysTableService;
import com.sirdc.modules.utils.NumberUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.supervision.task.dao.DeptTaskRanksDao;
import net.huashitong.supervision.task.dataBean.RankEvalDTO;
import net.huashitong.supervision.task.entity.DeptTaskRanksEntity;
import net.huashitong.supervision.task.entity.EvalRules;
import net.huashitong.supervision.task.entity.TaskMain;
import net.huashitong.supervision.task.entity.TaskUnit;
import net.huashitong.supervision.task.filter.EvalRulesFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class DeptTaskRanksService  extends StringPKService<DeptTaskRanksEntity> {
    @Autowired
    private DeptTaskRanksDao dao;
    @Autowired
    private SysTableService tableService;

    @Autowired
    private TaskUnitService taskUnitService;

    @Autowired
    private EvalRuleService evalRuleService;
    @Autowired
    private TaskMainService taskMainService;


    @Override
    protected BaseDao<DeptTaskRanksEntity, String> getDao() {
        return dao;
    }

    @Override
    protected void beforeSave(DeptTaskRanksEntity obj) {
        obj.setSysId(tableService.updateMaxSysId("t_task_dept_rank",null));
    }

    /**
     * 根据主任务id  拿到排名
     * @param taskMainId
     * @return
     */
   public List<DeptTaskRanksEntity> getRankBymainId(String taskMainId,String timeRound)
    {
        List<DeptTaskRanksEntity> listDeptRank = dao.getListDeptRank(taskMainId,timeRound);
        return listDeptRank;
    }

    public DeptTaskRanksEntity getOwnRank(String taskMainId,String timeRound,String deptId)
    {
        DeptTaskRanksEntity ownRank = dao.getOwnRank(deptId, timeRound, taskMainId);
        return ownRank;
    }

    /**
     * 保存排名
     * @param taskMainid
     * @return
     */
    public List<DeptTaskRanksEntity> saveDeptRank(String taskMainid,String timeRound)
    {
        List<TaskUnit> taskUnits = taskUnitService.getByMainId(taskMainid);
        List<DeptTaskRanksEntity> listRanks = new ArrayList<>();
        ArrayList<TaskUnit> collect = taskUnits.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(TaskUnit::getDeptId))), ArrayList::new));
        int i=1;
        for (TaskUnit item : collect) {
            DeptTaskRanksEntity entity  = new DeptTaskRanksEntity();
            entity.setDeptId(item.getDeptId());
            entity.setDeptName(item.getDeptName());
            entity.setDeptRank(i);
            entity.setMainTaskId(taskMainid);
            //save(entity);
            entity.setTimeRound(timeRound);
            listRanks.add(entity);
            i++;
        }
        this.batchSave(listRanks);
        return  listRanks;
    }

    /**
     * 计算排名范围
     */
    public RankEvalDTO calculateRank(int size, String eval)
    {
        EvalRulesFilter filter = new EvalRulesFilter();
        filter.setRuleId(eval);
        List<EvalRules> evalRules = evalRuleService.getEvalRules(filter);
        EvalRules goods = evalRules.stream().filter(evalRules1 -> evalRules1.getRuleCode().equals("good")).collect(Collectors.toList()).get(0);
        EvalRules normals = evalRules.stream().filter(evalRules1 -> evalRules1.getRuleCode().equals("normal")).collect(Collectors.toList()).get(0);
        EvalRules bads = evalRules.stream().filter(evalRules1 -> evalRules1.getRuleCode().equals("bad")).collect(Collectors.toList()).get(0);

        float goodRatio = Float.parseFloat(goods.getRuleRatio())/100;
        float normalRatio = Float.parseFloat(normals.getRuleRatio())/100;
        float badRatio = Float.parseFloat(bads.getRuleRatio())/100;

        RankEvalDTO dot = new RankEvalDTO();
        dot.setBad(badRatio);
        dot.setGood(goodRatio);
        dot.setNormal(normalRatio);

        float   goodRank   = NumberUtils.keepPrecision(size*goodRatio,2);
        float  badrank  = NumberUtils.keepPrecision(size*badRatio,2);
        float  normalrank = NumberUtils.keepPrecision(size* normalRatio,2);

        int nor =(int) normalrank ;
        int gd =(int ) goodRank;
        dot.setGoodRank(gd);
        dot.setNormalRank(nor+gd);
        int tem = size-nor-gd;
        dot.setBadRank(tem);


        return  dot;

    }

    //更新排名 应用评价
    public  List<DeptTaskRanksEntity> updateDeptRank(List<DeptTaskRanksEntity> list,String evaluations){
       // TaskMain mainTask = taskMainService.getById(taskMainId);
        //String evaluations = mainTask.getEvaluations();
        if(StringUtils.isBlank(evaluations))
        {
            //直接更新
            return  list;
        }

        RankEvalDTO rankEvalDTO = calculateRank(list.size(), evaluations);

        for (DeptTaskRanksEntity item : list) {
            if (item.getDeptRank()<=rankEvalDTO.getGoodRank()) {
                item.setEvaluations("好");
            }else if(item.getDeptRank()<=rankEvalDTO.getNormalRank()&&item.getDeptRank()>rankEvalDTO.getGoodRank())
            {
                item.setEvaluations("中");
            }else {
                item.setEvaluations("差");
            }
        }

        return  list;

    }


    public HashMap<String,Object> getTaskDeptRanks(String taskMainId)
    {


        List<DeptTaskRanksEntity> list = dao.queryRankByMainId(taskMainId);
        Map<String, List<DeptTaskRanksEntity>> collect = list.stream().collect(Collectors.groupingBy(DeptTaskRanksEntity::getDeptName));
        System.out.println(collect);
        Map<String, List<DeptTaskRanksEntity>> timeRoundMap = list.stream().collect(Collectors.groupingBy(DeptTaskRanksEntity::getTimeRound));

        List<String> rounds = new ArrayList<>();
        for(Map.Entry<String,List<DeptTaskRanksEntity>>  entry:timeRoundMap.entrySet())
        {
            rounds.add("第"+ entry.getKey()+"轮");
        }
        HashMap<String,Object> dataMap = new HashMap<>();
        dataMap.put("timeRound",rounds);
        dataMap.put("deptEvals",collect);

        return dataMap;

    }




}
